//
//  WDLifeStyleViewController.m
//  winterTest
//
//  Created by wd on 15/11/2.
//  Copyright © 2015年 Wei Chuang Le ,Ltd. All rights reserved.
//

#import "WDLifeStyleViewController.h"
#import "WDTestViewController.h"
#import "WDNavigationController.h"

@interface WDLifeStyleViewController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation WDLifeStyleViewController

- (void)loadView
{
    [super loadView];
    
    self.title = @"生活圈";
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self configureSelfUI];
}

// test timer
- (void)testTimerStart
{
    // test timer
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(testTimer:) userInfo:nil repeats:YES];
    //
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}

- (void)testTimer:(NSTimer *)timer
{
    NSLog(@"timer...");
}

- (void)configureSelfUI
{
    self.tableView = ({
        UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.showsVerticalScrollIndicator = NO;
        [self.view addSubview:tableView];
        [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view);
        }];
        tableView.tableFooterView = [UIView new];
        tableView;
    });
    
    
//    __weak typeof(self) weakSelf = self;
    // 刷新
//    ODRefreshControl *refreshControl = [[ODRefreshControl alloc]initInScrollView:self.tableView];
//    [refreshControl addTarget:self action:@selector(dropViewDidBeginRefresh:) forControlEvents:UIControlEventValueChanged];

//    [self.tableView addPullToRefreshWithActionHandler:^{
//        [weakSelf dropViewDidBeginRefresh:weakSelf.tableView.pullToRefreshView];
//    }];
    [self.tableView addPullToRefreshWithTarget:self refreshingAction:@selector(dropViewDidBeginRefresh:)];
    
    // 加载
    [self.tableView addInfiniteScrollingWithTarget:self infiniteScrollingAction:@selector(dropViewDidBeginRefresh:)];
//    [self.tableView addInfiniteScrollingWithActionHandler:^{
//        NSLog(@"tableView frame = %@",NSStringFromCGRect(weakSelf.tableView.frame));
//        double delayInSeconds = 3.0;
//        dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
//        dispatch_after(delayTime, dispatch_get_main_queue(), ^(void){
//            // code to be executed on the main queue after delay
//            [weakSelf.tableView.infiniteScrollingView stopAnimating];
//            weakSelf.tableView.infiniteScrollingView.enabled = NO;
//            weakSelf.tableView.showsInfiniteScrolling = NO;
//        });
//    }];
}

#pragma mark - 
#pragma mark - Refresh

//- (void)dropViewDidBeginRefresh:(ODRefreshControl *)refreshControl
//{
//    NSLog(@"refresh end");
//    
//    double delayInSeconds = 3.0;
//    dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
//    dispatch_after(delayTime, dispatch_get_main_queue(), ^(void){
//        // code to be executed on the main queue after delay
//        [refreshControl endRefreshing];
//        self.tableView.showsInfiniteScrolling = YES;
//    });
//}

- (void)dropViewDidBeginRefresh:(SVPullToRefreshView *)refreshControl
{
    NSLog(@"refresh end");
    
    double delayInSeconds = 2.0;
    dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(delayTime, dispatch_get_main_queue(), ^(void){
        // code to be executed on the main queue after delay
        [refreshControl stopAnimating];
    });
}

- (void)dropViewDidLoad:(SVInfiniteScrollingView *)infiniteControl
{
    NSLog(@"infiniteControl end");
    
    double delayInSeconds = 2.0;
    dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(delayTime, dispatch_get_main_queue(), ^(void){
        // code to be executed on the main queue after delay
        [infiniteControl stopAnimating];
        //        self.tableView.showsInfiniteScrolling = YES;
    });
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"cell - %ld",indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    WDTestViewController *testVC = [[WDTestViewController alloc]init];
    UINavigationController *nav = [[WDNavigationController alloc]initWithRootViewController:testVC];
    testVC.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:testVC action:@selector(dismissModalViewControllerAnimated:)];
    [self presentViewController:nav animated:YES completion:nil];
//    [self.navigationController pushViewController:testVC animated:YES];
}

// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
